package com.notary.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.notary.entity.*;
import com.notary.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.notary.entity.vo.JsonResult;
import com.notary.service.SysMenuService;

import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author xujun
 * @since 2020-07-28
 */


@Api(tags = "菜单权限表控制器")
@RequestMapping("/menu")
@RestController
public class SysMenuController {

    SysMenuService sysMenuService;
    SysRoleMenuService sysRoleMenuService;

    @Autowired
    public SysMenuController(SysMenuService sysMenuService,SysRoleMenuService sysRoleMenuService) {
        this.sysMenuService = sysMenuService;
        this.sysRoleMenuService = sysRoleMenuService;
    }

    /**
     * <p>
     * 根据条件查询全部记录（并翻页）
     * </p>
     */

    @ApiOperation(value = "查询所有菜单")
    @PostMapping("/list")
    //     @PreAuthorize("hasAnyAuthority(''sys:menu:list')")
    public JsonResult<IPage<SysMenu>> list(int pageNum, int pageSize) {
        Page<SysMenu> page = new Page<SysMenu>(pageNum, pageSize);
        IPage<SysMenu> iPage = sysMenuService.page(page, new QueryWrapper<>());
        return JsonResult.success(iPage);
    }


    @ApiOperation(value = "查询用户菜单")
    @PostMapping("/queryMenuByUserId")
    public JsonResult<List<SysMenu>> queryMenuByUserId(Long userId) {
        return JsonResult.success(sysMenuService.queryAllMenu(userId));
    }

    @ApiOperation(value = "查询用户的所有权限")
    @PostMapping("/queryAllPermsByUserId")
    public JsonResult<List<String>> queryAllPermsByUserId(Long userId) {
        return JsonResult.success(sysMenuService.queryAllPerms(userId));
    }
    /**
     * <p>
     * 根据ID查找返回单个对象
     * </P>
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/find/{id}")
    //     @PreAuthorize("hasAnyAuthority(''sys:menu:info')")
    public JsonResult<SysMenu> getById(@PathVariable("id") Integer id) {
        return JsonResult.success(sysMenuService.getById(id));
    }


    /**
     * <p>
     * 保存对象-如果对象存在则修改原数据，否则插入一条新数据
     * </p>
     */
    @ApiOperation(value = "保存对象")
    @GetMapping("/saveOrUpdate")
    //     @PreAuthorize("hasAnyAuthority(''sys:menu:save')")
    public JsonResult saveOrUpdate(@RequestBody SysMenu sysMenu) {
        return JsonResult.success(sysMenuService.saveOrUpdate(sysMenu));
    }


    /**
     * <p>
     * 根据 id删除记录-逻辑删除
     * </p>
     */
    @ApiOperation(value = "删除")
    @GetMapping("/remove/{id}")
    //     @PreAuthorize("hasAnyAuthority(''sys:menu:delete')")
    public JsonResult removeById(@PathVariable("id") Integer id) {
        return JsonResult.success(sysMenuService.deleteById(id));
    }


}

                       